package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.bs.transfer.transfer
 *
 * @author wangwenkai
 * @date 2017/10/19/11:07
 * E-mail：605064334@qq.com
 * 获取患者信息
 */
public class QueryPatientInfoTransfer extends AbstractTransfer {

    public QueryPatientInfoTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("QUERY_TYPE", "3").addAttribute("QUERY_CONJ", idCard);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Patient").element("DataRow");
            QueryPatientInfoInnerMessage queryPatientInfoInnerMessage = new QueryPatientInfoInnerMessage(dataRow);
            resMap.put("patientId", queryPatientInfoInnerMessage.getPatientId());
            resMap.put("age",queryPatientInfoInnerMessage.getAge());
            resMap.put("sex",queryPatientInfoInnerMessage.getSex());
            resMap.put("birthday",queryPatientInfoInnerMessage.getBirthday());
            resMap.put("name",queryPatientInfoInnerMessage.getName());
            resMap.put("patBind",queryPatientInfoInnerMessage.getPatBind());
            resMap.put("code", executeStatus);
            resMap.put("message", "成功！");
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    private class QueryPatientInfoInnerMessage implements InnerMessage {


        /**
         * 身份证号
         */
        private String idNo;
        /**
         * 姓名
         */
        private String name;
        /**
         * 年龄
         */
        private String age;
        /**
         * 生日
         */
        private String birthday;
        /**
         * 性别，中文（男、女、未知）
         */
        private String sex;
        /**
         * 住址
         */
        private String address;
        /**
         * 民族，中文（汉族，回族等）
         */
        private String nation;
        /**
         * 出生地，代码
         */
        private String birthPlace;
        /**
         * 邮政编码
         */
        private String zipCode;
        /**
         * 手机号码
         */
        private String phoneNumberHome;
        /**
         * 来源于婚姻状况字典，代码
         */
        private String maritalStatus;
        /**
         * 患者ID
         */
        private String patientId;
        /**
         * 绑定状态
         */
        private String patBind;


        public QueryPatientInfoInnerMessage(Element element) {

            this.idNo = element.attributeValue("ID_NO");
            this.name = element.attributeValue("NAME");
            this.age = element.attributeValue("AGE");
            this.birthday = element.attributeValue("BIRTHDAY");
            this.sex = element.attributeValue("SEX");
            this.address = element.attributeValue("ADDRESS");
            this.nation = element.attributeValue("NATION");
            this.birthPlace = element.attributeValue("BIRTH_PLACE");
            this.zipCode = element.attributeValue("ZIP_CODE");
            this.phoneNumberHome = element.attributeValue("PHONE_NUMBER_HOME");
            this.maritalStatus = element.attributeValue("MARITAL_STATUS");
            this.patientId = element.attributeValue("PATIENT_ID");
            this.patBind = element.attributeValue("PAT_BIND");
        }

        public String getIdNo() {
            return idNo;
        }

        public void setIdNo(String idNo) {
            this.idNo = idNo;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getBirthday() {
            return birthday;
        }

        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getNation() {
            return nation;
        }

        public void setNation(String nation) {
            this.nation = nation;
        }

        public String getBirthPlace() {
            return birthPlace;
        }

        public void setBirthPlace(String birthPlace) {
            this.birthPlace = birthPlace;
        }

        public String getZipCode() {
            return zipCode;
        }

        public void setZipCode(String zipCode) {
            this.zipCode = zipCode;
        }

        public String getPhoneNumberHome() {
            return phoneNumberHome;
        }

        public void setPhoneNumberHome(String phoneNumberHome) {
            this.phoneNumberHome = phoneNumberHome;
        }

        public String getMaritalStatus() {
            return maritalStatus;
        }

        public void setMaritalStatus(String maritalStatus) {
            this.maritalStatus = maritalStatus;
        }

        public String getPatientId() {
            return patientId;
        }

        public void setPatientId(String patientId) {
            this.patientId = patientId;
        }

        public String getPatBind() {
            return patBind;
        }

        public void setPatBind(String patBind) {
            this.patBind = patBind;
        }
    }
}
